<?php

class Expense extends AppModel {

    var $name = 'Expense';
    var $validate = array(
        'expensetype_id' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'value' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $belongsTo = array(
        'Expensetype' => array(
            'className' => 'Expensetype',
            'foreignKey' => 'expensetype_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Employee' => array(
            'className' => 'Employee',
            'foreignKey' => 'employee_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    function payAll($employee_id, $date_from, $date_to, $pagar_caja) {

        //si hay que registrar el movimiento en la caja
        if ($pagar_caja) {
            $sql = 'SELECT *
                FROM expenses as Expense
                WHERE paid = 0
                AND Expense.employee_id = ' . $employee_id . '
                AND Expense.date >= "' . $date_from . '"
                AND Expense.date <= "' . $date_to . '"';

            $expenses = $this->query($sql);
            
            //me traigo el modelo
            App::import('Model', 'CashMovement');
            $this->CashMovement = new CashMovement();

            foreach ($expenses as $expense) {
                $array_mov = array();
                $array_mov['user_id'] = $_SESSION['Auth']['User']['id'];
                $array_mov['type'] = 'out';
                if ($expense['Expense']['employee_id']) {
                    $array_mov['movement'] = 'employee';
                } else {
                    $array_mov['movement'] = 'expense';
                }
                $array_mov['amount'] = $expense['Expense']['value'];
                $array_mov['expense_id'] = $expense['Expense']['id'];
                $array_mov['employee_id'] = $expense['Expense']['employee_id'];

                //guardamos el movimiento
                $this->CashMovement->create();
                $this->CashMovement->save($array_mov);
            }
        }

        //ahora los marcamos como pagados
        $sql = 'UPDATE expenses as Expense
                SET paid = 1
                WHERE Expense.employee_id = ' . $employee_id . '
                AND Expense.date >= "' . $date_from . '"
                AND Expense.date <= "' . $date_to . '"';

        return $this->query($sql);
    }

}
